package com.scau.clothes.api.stock;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.scau.clothes.core.core.entity.AjaxResult;
import com.scau.clothes.stock.model.dto.ClothesDto;
import com.scau.clothes.stock.model.entity.Clothes;
import com.scau.clothes.stock.service.ClothesService;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/clothes")
@RestController
public class ClothesController {

    @Resource
    private ClothesService clothesService;

    @PostMapping("/listClothes")
    @ApiOperation("查询衣服")
    public AjaxResult listClothes(@RequestBody ClothesDto clothesDto){
        List<Clothes> list = clothesService.listClothes(clothesDto);
        return AjaxResult.success("查询成功",list);
    }

    @PostMapping("/insertClothes")
    @ApiOperation("新增衣服")
    public AjaxResult insertClothes(@RequestBody Clothes clothes, HttpSession session){
        // 会话、缓存或数据库表中获取URL
        String url = (String) session.getAttribute("uploadedFileUrl");
        // 设置URL到clothes对象
        clothes.setUrl(url);
        int rows = clothesService.insertClothes(clothes);
        // 如果更新成功，则从会话中移除URL
        if (rows > 0) {
            session.removeAttribute("uploadedFileUrl");
            return AjaxResult.success("新增服装成功");
        } else {
            // 如果更新失败，您可以选择是否移除URL，这取决于您的业务需求
            // session.removeAttribute("uploadedFileUrl"); // 可选
            return AjaxResult.error("新增服装失败");
        }
    }

    @PostMapping("/updateClothesInfo")
    @ApiOperation("更新衣服信息")
    public AjaxResult updateClothesInfo(@RequestBody Clothes clothes, HttpSession session){
        // 会话、缓存或数据库表中获取URL
        String url = (String) session.getAttribute("uploadedFileUrl");
        // 设置URL到clothes对象
        clothes.setUrl(url);
        int rows = clothesService.updateClothes(clothes);
        if (rows > 0) {
            session.removeAttribute("uploadedFileUrl");
            return AjaxResult.success("更新衣服信息成功");
        } else {
            // 如果更新失败，您可以选择是否移除URL，这取决于您的业务需求
            // session.removeAttribute("uploadedFileUrl"); // 可选
            return AjaxResult.error("更新衣服信息失败");
        }
    }

    @PostMapping("/deleteClothes")
    @ApiOperation("下架衣服")
    public AjaxResult deleteClothes(@RequestBody Clothes clothes){
        int rows = clothesService.deleteClothes(clothes);
        return rows > 0 ? AjaxResult.success("下架衣服成功") : AjaxResult.error("下架衣服失败");
    }

    @GetMapping("/page")
    @ApiOperation("分页操作")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, ClothesDto clothesDto){
        List<Clothes> list = clothesService.listClothes(clothesDto);
        Page page = new Page(pageNum, pageSize);
        int total = list.size();
        page.setTotal(total);
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize,total);
        page.addAll(list.subList(startIndex,endIndex));
        PageInfo pageInfo = new PageInfo<>(page);
        return AjaxResult.success(pageInfo);
    }
}
